﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TwitterAPI;
using VsSpeakerCompanion;
using System.Windows;
using Microsoft.VisualStudio.Text.Editor;
using System.ComponentModel.Composition;
using System.Windows.Input;
using VsSpeakerCompanion.UI;

namespace TwitterThisWidget
{
    /// <summary>
    /// Allows you to select some text in the code editor and send it using Twitter
    /// </summary>
    class Widget : IWidget
    {
        // our widget UI
        private TwitterThisView view;

        // services we will be making use of
        // for sending Twitter messages
        private TwitterService twitterService;
        // for tracking the active text view
        private IVisualStudioService visualStudioService;
        // to show messages to the user
        private IUIService uiService;

        public Widget(TwitterService twitterService, IVisualStudioService visualStudioService, IUIService uiService)
        {
            this.twitterService = twitterService;
            this.visualStudioService = visualStudioService;
            this.uiService = uiService;

            this.view = new TwitterThisView();
            // list to clicks on our widget
            this.view.Tweet += new EventHandler(OnViewTweet);
        }
            
        public UIElement VisualElement
        {
            get
            {
                // return our TweetThis! UI
                return view;
            }
        }

        public void Reset()
        {
            // this widget doesn't use any state that may need to be reset
        }

        void UpdateStatus(string newStatus)
        {
            // This uses the Twitter service to update the speaker Twitter status publishing the selected code
            this.uiService.ExecuteOperation(() => twitterService.UpdateStatus(twitterService.GetMessagesWithHashcode(newStatus)));
        }

        void OnViewTweet(object sender, EventArgs e)
        {
            // Retrieve the active text view
            IWpfTextView activeTextViewInstance = this.visualStudioService.GetActiveTextView();

            if (activeTextViewInstance != null)
            {
                // if there is a selection at all
                if (activeTextViewInstance.Selection.SelectedSpans.Count > 0)
                {
                    // Get the current selection of the active text view
                    string textToTweet = activeTextViewInstance.Selection.SelectedSpans[0].GetText();

                    if (!String.IsNullOrWhiteSpace(textToTweet))
                    {
                        // Tweet it!
                        // We don't need to check the length of the selection as our Twitter APIs support the sending of large messages by
                        // automatically splitting them in several separate messages.
                        UpdateStatus(textToTweet);
                    }
                }
                else
                {
                    // TODO: Move text to resource file for easier localization
                    // Tell the user it's not funny to twitter no text!
                    this.uiService.ShowMessage("There is nothing to tweet! Please select some code and click on the widget again.");
                }
            }
        }
    }
}